package com.changzhi.util;

import cn.hutool.core.util.RandomUtil;
import com.changzhi.common.exception.ServiceException;
import com.changzhi.ldyl.model.vo.NewLotteryVo;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description:
 * @author: 小落
 * @date: 2020年11月10日
 */
public class CommonUtil {

	public static List<String> TEST_ACCOUNT_LIST = Arrays.asList(new String[]{"1281524870147997696",
			"1351854801901584384",
			"1338348217682624512",
			"1295253461415231488",
			"1352101536284540928",
			"1372847767998693376",
			"1379380363373051904",
			"1379394145822441472",
			"1273949383372570624",
			"1291199336004190208",
			"1397827799774920704",
			"1407283683294969856",
			"1422444052552351744",
			"1268733552556507136"});



	public static List<String> TEST_MOBILE_LIST = Arrays.asList(new String[]{"18720134600",
			"13824552403",
			"13907793980",
			"15767581796",
			"13824552403",
			"13250545589"});


	/**
	 *
	 * @param userId
	 * @param targetUserId
	 * @return
	 */
	public static String getGroupId(String userId, String targetUserId) {
		String privateChannelId;
		if (userId.compareTo(targetUserId) > 0) {
			privateChannelId = userId + "_" + targetUserId;
		} else {
			privateChannelId = targetUserId + "_" + userId;
		}
		return privateChannelId;
	}

	/**
	 * 抽奖概率，概率不带小数点
	 *
	 * @return
	 */
	public static NewLotteryVo lotteryHigh(List<NewLotteryVo> lotteryList) {
		// 计算所有概率的总和
		Integer sum = lotteryList.stream().mapToInt(t -> ((t.getProbability()))).sum();
		NewLotteryVo prize = null;
		int rate = 0;
		int random = RandomUtil.randomInt(0, sum);
		for (NewLotteryVo item : lotteryList) {
			// 中奖概率
			rate += Integer.valueOf(item.getProbability());
			if (random < rate) {
				prize = item;
				break;
			}
		}
		return prize;
	}

	/**
	 *
	 * 生产环境测试号token生成
	 * @return
	 */
	public static List<String> getTestAccountList() {
		return TEST_ACCOUNT_LIST;
	}

	public static List<String> getTestMobileList() {
		return TEST_MOBILE_LIST;
	}

	/**
	 * 获取随机邀请码
	 *
	 * @param randomLength 邀请码总长度
	 * @param letterLength 邀请码中包含的字母长度
	 * @return
	 */
	public static String genRandomCode(int randomLength, int letterLength) {
		if (letterLength > randomLength)
			letterLength = randomLength;
		int count = letterLength;
		String[] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
		String[] nums = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };// 排除数字0
		List<String> codes = new ArrayList<>(randomLength);
		Random random = new Random();
		while (count > 0) {
			int index = Math.abs(random.nextInt(letters.length));
			if (index >= 0 && index < letters.length) {
				codes.add(letters[index]);
				--count;
			}
		}
		int numLength = randomLength - letterLength;
		while (numLength > 0) {
			int index = Math.abs(random.nextInt(nums.length));
			if (index >= 0 && index < nums.length) {
				codes.add(nums[index]);
				--numLength;
			}
		}
		Collections.shuffle(codes);
		String code = String.join("", codes);
		return code;
	}

	public static String urlEncodeChinese(String url) {
		try {
			Matcher matcher = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(url);

			for (String tmp = ""; matcher.find(); url = url.replaceAll(tmp, URLEncoder
					.encode(tmp, "UTF-8"))) {
				tmp = matcher.group();
			}
		} catch (UnsupportedEncodingException var3) {
			var3.printStackTrace();
		}

		return url.replace(" ", "%20");
	}

	/**
	 * 是否同号、连号
	 * @param str
	 * @return
	 */
	public static boolean checkSame(String str) {
		boolean b = true;
		//是否同号吗
		char[] c = str.toCharArray();
		for (int i = 0; i < c.length - 1; i++) {
			if (c[i + 1] - c[i] != 0) {
				b = false;
				break;
			}
		}
		//是连号吗
		if(!b){
			b = true;
			for (int i = 0; i < c.length - 1; i++) {
				if (c[i + 1] - c[i] != 1) {
					b = false;
					break;
				}
			}
		}
		return b;
	}

	public static void checkPwd(String pwd){
		if(!"8704".equals(pwd)){
			throw new ServiceException("密码错误！");
		}
	}

}
